﻿//这个文件保存ajax传递与返回的信息事件函数
function ajaxSuccess(xml) {
    successRaceTitle(xml);
}

function ajaxError(xml) {
    alert('数据读取失败!' + xml);
}

function ajaxMsgfs(str_msg, str_data) {
    //var txt = @Url.RouteUrl(new { controller = "GameForm", action = "ClientMsg" });
    //alert(txt);
    $.ajax({
        url: 'ClientMsg',//'@Url.RouteUrl(new { controller = "GameForm", action = "ClientMsg" })',//'GameForm/ClientMsg',
        type: 'GET',
        data: { msg: str_msg, data: str_data },
        dataType: 'xml', //这里可以不写，但千万别写text或者html!!!
        timeout: 1000,
        error: ajaxError,
        success: ajaxSuccess
    });
    //alert("d");
}

var STR;
var DEX;
var CON;
var INT;
var WIS;
var CHA;
var MSTR;
var MDEX;
var MCON;
var MINT;
var MWIS;
var MCHA; 
var SSTR;
var SDEX;
var SCON;
var SINT;
var SWIS;
var SCHA;
var Points;
function successRaceTitle(xml) {
    $(xml).find("RaceInfo").each(function (i) {
        STR = $(this).attr("STR");
        DEX = $(this).attr("DEX");
        CON = $(this).attr("CON");
        INT = $(this).attr("INT");
        WIS = $(this).attr("WIS");
        CHA = $(this).attr("CHA");
        MSTR = $(this).attr("MSTR");
        MDEX = $(this).attr("MDEX");
        MCON = $(this).attr("MCON");
        MINT = $(this).attr("MINT");
        MWIS = $(this).attr("MWIS");
        MCHA = $(this).attr("MCHA");
        SSTR = $(this).attr("SSTR");
        SDEX = $(this).attr("SDEX");
        SCON = $(this).attr("SCON");
        SINT = $(this).attr("SINT");
        SWIS = $(this).attr("SWIS");
        SCHA = $(this).attr("SCHA");
        Points = $(this).attr("Points")

        $("#STR").attr("value", STR);
        $("#DEX").attr("value", DEX);
        $("#CON").attr("value", CON);
        $("#INT").attr("value", INT);
        $("#WIS").attr("value", WIS);
        $("#CHA").attr("value", CHA);

        $("#STR").attr("min", SSTR);
        $("#DEX").attr("min", SDEX);
        $("#CON").attr("min", SCON);
        $("#INT").attr("min", SINT);
        $("#WIS").attr("min", SWIS);
        $("#CHA").attr("min", SCHA);
        $("#STR").attr("max", MSTR);
        $("#DEX").attr("max", MDEX);
        $("#CON").attr("max", MCON);
        $("#INT").attr("max", MINT);
        $("#WIS").attr("max", MWIS);
        $("#CHA").attr("max", MCHA);

        $("#RaceTitle").attr("value", $(this).attr("Title"));
        document.getElementById("LPoinst").innerHTML = Points;

        //$("#RaceID").attr("value", $("#RaceID").val());

    });
}
function randomPros() {
    if ($("#Race").val() != 0) {
        ajaxMsgfs("race", $("#RaceID").val());
    }
}

function setPoints(value) {
    Points = Number(Points) - value;
    document.getElementById("LPoinst").innerHTML = Points;
}

function changeStr() {
    var value = Number($("#STR").val() - STR);
    if (Points - value < 0) {
        STR = Number(STR) + Number(Points)
        $("#STR").attr("value", STR);
        value = Number(Points);
    }
    STR = Number($("#STR").val());
    setPoints(value);
}
function changeDex() {
    var value = Number($("#DEX").val() - DEX);
    if (Points - value < 0) {
        DEX = Number(DEX) + Number(Points)
        $("#DEX").attr("value", DEX);
        value = Number(Points);
    }
    DEX = Number($("#DEX").val());
    setPoints(value);
}
function changeCon() {
    var value = Number($("#CON").val() - CON);
    if (Points - value < 0) {
        CON = Number(CON) + Number(Points)
        $("#CON").attr("value", CON);
        value = Number(Points);
    }
    CON = Number($("#CON").val());
    setPoints(value);
}
function changeInt() {
    var value = Number($("#INT").val() - INT);
    if (Points - value < 0) {
        INT = Number(INT) + Number(Points)
        $("#INT").attr("value", INT);
        value = Number(Points);
    }
    INT = Number($("#INT").val());
    setPoints(value);
}
function changeWis() {
    var value = Number($("#WIS").val() - WIS);
    if (Points - value < 0) {
        WIS = Number(WIS) + Number(Points)
        $("#WIS").attr("value", WIS);
        value = Number(Points);
    }
    WIS = Number($("#WIS").val());
    setPoints(value);
}
function changeCha() {
    var value = Number($("#CHA").val() - CHA);
    if (Points - value < 0) {
        CHA = Number(CHA) + Number(Points)
        $("#CHA").attr("value", CHA);
        value = Number(Points);
    }
    CHA = Number($("#CHA").val());
    setPoints(value);
}

$(document).ready(function () {
    $("#RaceID").change(function () { randomPros(); });

    //设置事件,当选中或者改变值时,限制其改变量
    $("#STR").on("focusout", function () { changeStr(); })
    $("#DEX").on("focusout", function () { changeDex(); })
    $("#CON").on("focusout", function () { changeCon(); })
    $("#INT").on("focusout", function () { changeInt(); })
    $("#WIS").on("focusout", function () { changeWis(); })
    $("#CHA").on("focusout", function () { changeCha(); })
});
